var dataJson = [];
$(function(){
	loadData();
	$("#contractDatagrid").datagrid({
		data:{"rows":dataJson},
	    fit:true,
	    toolbar:'#contractTolbar',
	    fitColumns:true,
	    singleSelect:true,
	    columns:[[  
	        {field:'agreementId',hidden:true,title:'ID',width:100,align:"center"},
	        {field:'userName',title:'姓名',width:100,align:"center"}, 
	        {field:'phone',title:'电话',width:100,align:"center"}, 
	        {field:'clothingName',title:'服装名称',width:100,align:"center"}, 
	        {field:'clothingPrice',title:'服装价格',width:100,align:"center"}, 
	        {field:'clothingRent',title:'租赁价格',width:100,align:"center"}, 
	        {field:'foregift',title:'押金',width:100,align:"center"}, 
	        {field:'count',title:'数量',width:100,align:"center"}, 
	        {field:'leaseTime',title:'租赁时间',width:100,align:"center"}, 
	        {field:'returnTime',title:'归还时间',width:100,align:"center"}, 
	        {field:'operation',title:'操作',width:100,align:"center",
	        	formatter:function(value,row,index){
	        		return " <a  href='#' onclick=editRow("+index+");  >详细信息</a> ";
	        	}
	        	
	        },
	    ]]  
	
	});
	
})

function loadData(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Agreement/getAgreementList.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"userName":'',"agreementId":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.agreementList;;
		}
	})
}
function editRow(index){
	$("#editDialog").dialog("center");
	$("#editDialog").dialog("open");

	$('#contractDatagrid').datagrid("selectRow",index);
	var row = $('#contractDatagrid').datagrid("getSelected");
	if(!row){
		$('#contractDatagrid').datagrid("selectRow",index);
		row = $('#contractDatagrid').datagrid("getSelected");
	}
	
	window.location.href=$WEB_ROOT_PATH+"/jsp/contractManage/contractInfo.jsp?id="+row.agreementId;
}


function doSearch(){
	$.ajax({
		url:$WEB_ROOT_PATH+"/Agreement/getAgreementList.json",
		type:"post",
		async: false, 
		data: JSON.stringify({"userName":$("#contractName").val(),"agreementId":''}),
		contentType: "application/json",
		dataType:"json",
		success:function(result){
			dataJson = result.data.agreementList;
			$("#contractDatagrid").datagrid("loadData",dataJson);
		}
	})
}